#include "cadretraintab.h"
#include "ui_cadretraintab.h"
#include <QDialog>
#include <QGridLayout>
#include <QLineEdit>
#include <QSqlQuery>
#include <QPushButton>
#include <QSqlTableModel>
#include "timeeditdelegate.h"
#include <QDebug>
#include <QLabel>
#include <QDialogButtonBox>
#include <QMessageBox>
#include <QSqlError>
#include "cdateedit.h"
#include "cadreutil.h"

CadreTrainTab::CadreTrainTab(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CadreTrainTab)
{
    ui->setupUi(this);
}

CadreTrainTab::~CadreTrainTab()
{
    delete ui;
}
void CadreTrainTab::initTrainTableView(QString curPerson)
{
    trainModel = new QSqlTableModel(this);
    trainModel->setTable("Train");
    trainModel->setEditStrategy(QSqlTableModel::OnFieldChange);// OnRowChange OnFieldChange OnManualSubmit
    QString filter = QString::fromUtf8("IDNum='%1'").arg(curPerson);
    currentPerson=curPerson;
    trainModel->setFilter(filter);
    trainModel->select();
    trainModel->setHeaderData(1, Qt::Horizontal, QString::fromUtf8("开始时间"));
    trainModel->setHeaderData(2, Qt::Horizontal, QString::fromUtf8("结束时间"));
    trainModel->setHeaderData(3, Qt::Horizontal, QString::fromUtf8("培训类型"));
    trainModel->setHeaderData(4, Qt::Horizontal, QString::fromUtf8("培训方式"));
    trainModel->setHeaderData(5, Qt::Horizontal, QString::fromUtf8("培训机构"));
    trainModel->setHeaderData(6, Qt::Horizontal, QString::fromUtf8("培训内容"));
    trainModel->setHeaderData(7, Qt::Horizontal, QString::fromUtf8("组织结构"));
    ui->trainTableView->horizontalHeader()->setVisible(true); //UI界面设置，总是恢复为false
    ui->trainTableView->setModel(trainModel);
    TimeEditDelegate *starttime = new TimeEditDelegate();
    TimeEditDelegate *endtime = new TimeEditDelegate();
    ui->trainTableView->setItemDelegateForColumn(1,starttime);
    ui->trainTableView->setItemDelegateForColumn(2,endtime);
    ui->trainTableView->setColumnHidden(0,true);
    ui->trainTableView->setColumnHidden(8,true);
    ui->trainTableView->setColumnWidth(1,130);
    ui->trainTableView->setColumnWidth(2,130);
    ui->trainTableView->setColumnWidth(3,130);
    ui->trainTableView->setColumnWidth(4,130);
    ui->trainTableView->setColumnWidth(5,130);
    ui->trainTableView->setColumnWidth(6,130);
    ui->trainTableView->setColumnWidth(7,130);
    ui->trainTableView->selectRow(trainModel->rowCount()-1);
}
void CadreTrainTab::setButtonEnable(bool i)
{
    ui->trainAddButton->setEnabled(i);
    ui->trainDeleteButton->setEnabled(i);
    ui->trainDownButton->setEnabled(i);
    ui->trainUpButton->setEnabled(i);
    if(i){
        ui->trainTableView->setEditTriggers(QAbstractItemView::DoubleClicked);
    }else{
        ui->trainTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}

void CadreTrainTab::on_trainAddButton_clicked()
{
    QDialog *addDialog = new QDialog(this);
    addDialog->setWindowTitle(trUtf8("添加培训"));
    addDialog->setGeometry(700,300,600,380);
    addDialog->setContentsMargins(10,0,10,0);
    QGridLayout *gridLayout = new QGridLayout(addDialog);
    gridLayout->setMargin(10);

    QLabel *stimeLabel =new QLabel(trUtf8("开始时间"),addDialog);
    QLabel *etimeLabel=new QLabel(trUtf8("结束时间"),addDialog);
    QLabel *typeLabel=new QLabel(trUtf8("培训类型"),addDialog);
    QLabel *wayLabel=new QLabel(trUtf8("培训方式"),addDialog);
    QLabel *trainorganLabel=new QLabel(trUtf8("培训机构"),addDialog);
    QLabel *traincontentLabel=new QLabel(trUtf8("培训内容"),addDialog);
    QLabel *organstructureLabel=new QLabel(trUtf8("组织结构"),addDialog);

    CDateEdit *stime = new CDateEdit(addDialog);
    stime->setNullable(true);
    stime->setDisplayFormat("yyyy-MM-dd");
    stime->setCalendarPopup(true);
    stime->setDate(QDate());
    CDateEdit *etime = new CDateEdit(addDialog);
    etime->setNullable(true);
    etime->setDisplayFormat("yyyy-MM-dd");
    etime->setCalendarPopup(true);
    etime->setDate(QDate());
    QLineEdit *type = new QLineEdit(addDialog);
    QLineEdit *way = new QLineEdit(addDialog);
    QLineEdit *trainorgan = new QLineEdit(addDialog);
    QLineEdit *traincontent = new QLineEdit(addDialog);
    QLineEdit *organstructure = new QLineEdit(addDialog);

    gridLayout->addWidget(stimeLabel,0,0);
    gridLayout->addWidget(stime,0,1);
    gridLayout->addWidget(etimeLabel,0,2);
    gridLayout->addWidget(etime,0,3);
    gridLayout->addWidget(typeLabel,1,0);
    gridLayout->addWidget(type,1,1);
    gridLayout->addWidget(wayLabel,1,2);
    gridLayout->addWidget(way,1,3);
    gridLayout->addWidget(trainorganLabel,2,0);
    gridLayout->addWidget(trainorgan,2,1);
    gridLayout->addWidget(traincontentLabel,2,2);
    gridLayout->addWidget(traincontent,2,3);
    gridLayout->addWidget(organstructureLabel,3,0);
    gridLayout->addWidget(organstructure,3,1);


    QPushButton *yesButton = new QPushButton(trUtf8("确定"));
    QPushButton *cancelButton = new QPushButton(trUtf8("取消"));

    QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal);
    buttonBox->addButton(yesButton, QDialogButtonBox::AcceptRole);
    buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);

    gridLayout->addWidget(buttonBox,4,1,1,2,Qt::AlignHCenter);

    connect(buttonBox, SIGNAL(accepted()), addDialog, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), addDialog, SLOT(reject()));

    if(addDialog->exec()==QDialog::Accepted){
        QSqlQuery query;
        query.prepare("insert into Train (`StartTime`, `EndTime`, `TrainType`,"
                      " `TrainWay`,`TrainOrgan`, `TrainContent`, `OrganStructure`, `IDNum`)"
                      " values(:stime, :etime, :type, :way, :trainorgan, :traincontent, "
                      ":organstructure, :idnum )");
        query.bindValue(":stime",stime->date());
        query.bindValue(":etime",etime->date());
        query.bindValue(":type",type->text());
        query.bindValue(":way",way->text());
        query.bindValue(":trainorgan",trainorgan->text());
        query.bindValue(":traincontent",traincontent->text());
        query.bindValue(":organstructure",organstructure->text());
        query.bindValue(":idnum",currentPerson);
        if(query.exec()){
            QMessageBox::information(this,trUtf8("提示"),
               trUtf8("插入数据成功"),QMessageBox::Yes);
            initTrainTableView(currentPerson);
        }else{
            QString info = query.lastError().databaseText();
            QMessageBox::information(this,trUtf8("插入数据库错误"),
               info,QMessageBox::Yes);
        }
    }
}

void CadreTrainTab::on_trainDeleteButton_clicked()
{
    CadreUtil::deleteRow(trainModel, ui->trainTableView);
}

void CadreTrainTab::on_trainUpButton_clicked()
{
    int cur = CadreUtil::moveUpOne(trainModel,0,ui->trainTableView);
    initTrainTableView(currentPerson);
    ui->trainTableView->selectRow(cur);
}

void CadreTrainTab::on_trainDownButton_clicked()
{
    int cur = CadreUtil::moveDownOne(trainModel,0,ui->trainTableView);
    initTrainTableView(currentPerson);
    ui->trainTableView->selectRow(cur);
}
